package com.ztesoft.sca.dao;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author hxz
 */
@Component
public class IomManageDaoImpl implements IomManageDao {

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public IomManageDaoImpl(@Qualifier("secondaryJdbcTemplate")JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    @Override
    public List listIvrRecord() throws Exception {
        StringBuffer sql=new StringBuffer(" SELECT * FROM ( SELECT a.* FROM data_record_file A " )
                .append("left join data_record_file_convert b on a.id=b.id WHERE b.state is null ")
                .append("  ORDER BY A.CREATE_DATE ) ");

        List list = this.jdbcTemplate.queryForList(sql.toString());
        return list;
    }

    @Override
    public void addConvertRecord(Map param) throws Exception {
        String sql = " insert into data_record_file_convert values(?,'10I',?,?,?,?,?,sysdate,?) ";

        Object[] param1 = new Object[]{MapUtils.getString(param,"id"), MapUtils.getString(param,"file_name"),
                MapUtils.getString(param,"file_path"), MapUtils.getString(param,"order_code"),
                MapUtils.getString(param,"order_id"),MapUtils.getString(param,"acc_nbr"),
                MapUtils.getString(param,"type")};
        this.jdbcTemplate.update(sql, param1);
    }

    @Override
    public void updateConvertRecord(Map param) throws Exception {
        String sql = " update data_record_file_convert set state ='10F' WHERE ID=? ";

        Object[] param1 = new Object[]{MapUtils.getString(param,"id")};
        this.jdbcTemplate.update(sql, param1);
    }

    @Override
    public Map<String, Object> qryOrderInfo(Long fileId) throws Exception {
        StringBuffer sql=new StringBuffer(" SELECT TO_CHAR(A.calling_time, 'yyyy-MM-dd hh24:mi:ss') as CALL_TIME,")
                .append( " TO_CHAR(C.BE_BOK_DATE, 'yyyy-MM-dd hh24:mi:ss') as BOOK_TIME, ")
                .append("  TO_CHAR(C.END_BOK_DATE, 'yyyy-MM-dd hh24:mi:ss') as BOOK_END_TIME,")
                .append("  oos.order_state_name AS ORDER_STATUS," )
                .append("  TO_CHAR(D.APP_DATE, 'yyyy-MM-dd hh24:mi:ss') as HANDUP_TIME,")
                .append("  TO_CHAR(D.APP_UNHAND_DATE, 'yyyy-MM-dd hh24:mi:ss') as HANDUP_END_TIME ")
                .append("  FROM DATA_RECORD_FILE A" )
                .append("  JOIN OM_ORDER B ON A.ORDER_ID = B.ID" )
                .append("  join om_order_state oos on oos.order_state=b.order_state" )
                .append("  left join WFM_BOK_TRACK C on c.order_id = a.order_id" )
                .append("                      and c.track_id in" )
                .append("                          (select track_id" )
                .append( "                             from (select wbt.track_id" )
                .append( "                                     from WFM_BOK_TRACK wbt" )
                .append( "                                     join DATA_RECORD_FILE drf on wbt.order_id=drf.order_id" )
                .append("                                    where wbt.TRACK_DATE > drf.calling_time" )
                .append("                                      and wbt.ORDER_ID = drf.ORDER_ID and (wbt.TRACK_DATE-drf.calling_time)*24*60<10 and drf.id=? " )
                .append("                                    order by wbt.TRACK_DATE asc)" )
                .append("                            where rownum = 1)" )
                .append("  left join WFM_HAND_APPLICATION d on d.order_id = a.order_id" )
                .append("                      and d.app_id in" )
                .append( "                          (select app_id" )
                .append("                             from (select wha.app_id" )
                .append("                                     from WFM_HAND_APPLICATION wha" )
                .append("                                     join DATA_RECORD_FILE drf on wha.order_id=drf.order_id" )
                .append("                                    where wha.APP_DATE > drf.calling_time" )
                .append("                                      and wha.ORDER_ID = drf.ORDER_ID and (wha.APP_DATE-drf.calling_time)*24*60<10 and drf.id=? " )
                .append( "                                    order by wha.APP_DATE asc)" )
                .append( "                            where rownum = 1)                            " )
                .append(" WHERE A.ID = ? ");

        Object[] param = new Object[]{fileId,fileId,fileId};
        Map<String, Object> map = this.jdbcTemplate.queryForMap(sql.toString(),param);
        return map;
    }
}
